const int NEW_LINE = 10; int servo = 5; void setup(){ Serial.begin(9600); pinMode(servo, OUTPUT); } void loop(){ if(Serial.available()>0){ String command = Serial.readStringUntil(NEW_LINE); Serial.println(command.toInt()); servo_pwm(servo, command.toInt()); } } void servo_pwm(int pin, int dur){ int LoopCount = 25; // setting this lower than 25, will cause power instabilities for(int i = 0; i < LoopCount; ++i){ digitalWrite(pin, HIGH); delayMicroseconds(dur); digitalWrite(pin, LOW); delayMicroseconds(20000 - dur); } }